package cn.wk.dao.impl;

import cn.wk.dao.CourseDao;
import cn.wk.entity.Classes;
import cn.wk.entity.Course;
import cn.wk.util.JDBCUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CourseDaoImpl implements CourseDao {
    private JdbcTemplate template=new JdbcTemplate(JDBCUtil.getDataSource());
    @Override
    public List<Course> getAllCourse(Object... obj) {
        String sql="select c.couid,c.couname,c.tid,t.tname from course c,teacher t where c.tid=t.tid limit ?,?";
        List<Course> list=  template.query(sql, new BeanPropertyRowMapper<>(Course.class),obj);
        return list;
    }

    @Override
    public int AddCourse(Object... obj) {
        String sql="insert course values(null,?,?)";
        int b=template.update(sql,obj);
        return b;
    }

    @Override
    public int MidffCourse(Object... obj) {
        String sql="update course set couname=?,tid=? where couid=?";
        int b=template.update(sql,obj);
        return b;
    }

    @Override
    public int DelCourse(Object... obj) {
        String sql="delete from course where couid=?";
        int b=template.update(sql,obj);
        return b;
    }

    @Override
    public int AddAllCourse(String sql) {
        int b=template.update(sql);
        return b;
    }

    @Override
    public Classes findCourseById(int aid) {
        return null;
    }

    @Override
    public int CountSize() {
        String sql="select count(*) from course";
        int b=template.queryForObject(sql,Integer.class);
        return b;
    }

    @Override
    public List<Course> getCounameByTno(String tno) {
        String sql="select couname from  course c,teacher t " +
                " where c.tid=t.tid and t.tno=?";
        List<Course> list=  template.query(sql, new BeanPropertyRowMapper<>(Course.class),tno);
        return list;
    }
}
